<template>
  <div class="mod-config">
    <!-- 查询条件 -->
    <div class="search-form search_form" v-loading='loadingPage'>
      <el-form :model="searchForm" size="mini" label-position="right" class="demo-form-inline" label-width="110px">
        <el-row :gutter="10">
          <el-col :span="4">

            <el-form-item label="部门：" prop="deptId">
              <el-select v-model="searchForm.deptId" placeholder="请选择" filterable clearable>
                <el-option
                  v-for="(item ,index) in  deptList"
                  :key="index"
                  :label="item.deptName"
                  :value="item.deptId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="考核内容：">
              <el-select v-model="searchForm.assessmentContent" placeholder="请选择" filterable clearable>
                <el-option
                  v-for="item  in  assessmentContentList"
                  :key="item"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="类别：">
              <el-select v-model="searchForm.category" placeholder="请选择" filterable clearable>
                <el-option
                  v-for="item  in  categoryList"
                  :key="item"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="启动状态：">
              <el-select v-model="searchForm.status" placeholder="请选择" filterable clearable>
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" style="text-align:center;">
            <el-button size="mini" type="primary" plain @click="searchBtn">查询</el-button>
            <el-button size="mini" type="info" plain @click="emptyClick">清空</el-button>

          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-col
        :span="8"
        style="text-align:left; margin-bottom: 10px;"
      >
        <el-button
          v-hasPermi="['/permission/deperuledets']"
          size="mini"
          type="danger"
          :disabled="dataListSelections.length <= 0"
          @click="deleteHandle()"
        >
          批量删除
        </el-button>
        <el-button
          v-hasPermi="['/permission/deperuleimport']"
          size="mini"
          type="success"
          @click="exportOr"
        >导入
        </el-button>
      </el-col>
    </div>
    <el-table
      :data="tableData"
      border
      v-loading="loading"
      @selection-change="handleSelectionChange"
      :row-key="bindRowKeys"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="deptName"
        header-align="center"
        align="center"
        label="部门">
      </el-table-column>
      <el-table-column
        prop="assessmentContent"
        header-align="center"
        align="center"
        label="考核内容">
      </el-table-column>
      <el-table-column
        prop="category"
        header-align="center"
        align="center"
        label="类别">
      </el-table-column>
      <el-table-column
        prop="standard"
        header-align="center"
        align="center"
        label="标准">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="启用状态">
        <template slot-scope="scope">
          <span style="color: #13ce66 ;font-weight:bold" v-if="scope.row.status === 1">启用</span>
          <span style="color: #ff4949 ;font-weight:bold" v-if="scope.row.status === 0">未启用</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
        width="150"
      >
        <template slot-scope="scope">
          {{ new Date(scope.row.createTime).toLocaleDateString().replaceAll('/', '-') }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['/permission/deperuleUpdate']"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['/permission/deperuledel']"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="progress_lok_page">
      <page
        :hidden="tableData && tableData.length === 0"
        :total="total"
        :page.sync="pageNo"
        :limit.sync="pageSize"
        @pagination="handlePageChange"
      />
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></add-or-update>

    <el-dialog title="导入" :close-on-click-modal="false" :visible.sync="importWinVisible">
      <el-form label-width="100px" v-loading="importLoading">
        <el-row :gutter="10">
          <el-col :xs="24">
            <el-upload class="upload-demo" ref="upload"
                       :multiple="false" :file-list="fileList" :auto-upload="false"
                       :on-change="fileChange"
                       action="/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/importRule"
                       :on-success="handleUploadSuccess">
              <el-button type="danger" @click="downTemplate">下载模版</el-button>
              <el-button slot="trigger" type="primary">选取文件</el-button>
              <el-button style="margin-left: 10px;" type="success" @click="submitUpload">上传并导入</el-button>
              <div slot="tip" class="el-upload__tip">只能上传xlsx/xls文件</div>
            </el-upload>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  import AddOrUpdate from './deptexaminelrules-add-or-update'
  import axios from 'axios'
  import { Encrypt } from '../../utils/cryptoAES'
  import { AesDecrypt } from '../../utils/AesDecrypt'

  export default {
    data() {
      return {
        assessmentContentList: [],
        categoryList: [],
        deptList: [],
        importLoading: false,
        importWinVisible: false,
        visible: false,
        dataListSelections: [],
        fileList: [],
        addOrUpdateVisible: false,
        tableData: [],
        loadingPage: true,
        loading: true,
        currentPage: 0,
        pageNo: 1,
        pageSize: 10,
        total: 0,
        checkedArr: [],//要导出的数据数组
        searchForm: {
          assessmentContent: null,// 考核内容
          category: null,// 类别
          deptId: null,//部门名称
          status: -1,// 状态
          sortField: '',
          sort: '',
          ids: [] //导出数据集合 空为全部
        },
        options: [{
          value: -1,
          label: '全部'
        }, {
          value: 0,
          label: '未启用'
        }, {
          value: 1,
          label: '启用'
        }
        ]
      }
    },
    components: {
      AddOrUpdate
    },
    created() {
      this.getList ()
      this.getDeptList ()
      this.getAssessmentContent ()
      this.getAategory ()
    },
    methods: {

      getList() {
        var uu = {
          page: this.pageNo,
          pageSize: this.pageSize,
          deptId: this.searchForm.deptId,//部门
          ruleName: this.searchForm.ruleName,//规则名称
          status: this.searchForm.status,
          assessmentContent: this.searchForm.assessmentContent,
          category: this.searchForm.category
        }
        var jsonString = JSON.stringify (uu)
        var yy = Encrypt (jsonString)
        var encodedString = encodeURIComponent (yy)

        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/list',
          data: encodedString
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.tableData = data.result.content
            this.total = data.result.total
            this.loading = false
            this.loadingPage = false
          } else {
            this.loading = false
            this.loadingPage = false
            this.$message.error (data.msg)
            this.tableData = []
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 查询
      searchBtn() {
        this.loading = true
        this.page = 1
        this.getList ()
      },
      /** 点击分页触发的事件 */
      handlePageChange(pageInfo) {
        this.loading = true
        this.pageNo = pageInfo.page
        this.pageSize = pageInfo.limit
        this.getList ()
      },
      // 清空
      emptyClick() {
        this.searchForm.deptId = ''
        this.searchForm.assessmentContent = ''
        this.searchForm.category = ''
        this.searchForm.status = -1
      },

      bindRowKeys(row) {
        return row.id
      },

      // 导入
      exportOr() {
        this.importWinVisible = true
        this.isUpload = false
      },

      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick (() => {
          this.$refs.addOrUpdate.init (id)
        })
      },
      // 多选
      handleSelectionChange(val) {
        this.dataListSelections = val
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map (item => {
          return item.id
        })
        this.$confirm (`确定对[id=${ids.join (',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then (() => {
          axios ({
            url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/batchDelete',
            method: 'post',
            data: {
              ids: ids
            }
          }).then (({ data }) => {
            if (data && data.code === '00000') {
              this.$message ({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getList ()
                }
              })
            } else {
              this.$message.error (data.msg)
            }
          }).catch (error => {
            this.$message.error ('服务器访问异常，请从新登录')
          })
        })
      },
      submitUpload() {
        this.$refs.upload.submit ()
      },

      // 判断是否选择文件
      fileChange(file, fileList) {
        if (file) {
          this.isUpload = true
        }
      },

      // 上传
      submitUpload() {
        var _this = this
        if (!_this.isUpload) {
          this.$message.warning ('请先选取文件')
          return
        }
        _this.isUpload = false
        _this.importLoading = true
        _this.$refs.upload.submit ()
      },
      handleUploadSuccess(response, file, fileList) {
        this.importLoading = false
        this.$refs.upload.clearFiles ()
        if (response.code === '00000') {
          this.isUpload = false
          this.importWinVisible = false
          this.$message.info ('导入成功')
          this.reloadTable ()
        } else {
          this.$message.warning (response.message)
        }
      },
      // 刷新
      reloadTable() {
        // 执行刷新操作
        this.getList ()
      },
// 下载模版
      downTemplate() {
        axios ({
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/downloadTemp',
          method: 'post',
          responseType: 'blob'
        }).then (({ data }) => {
          if (data.size) {
            const downloadElement = document.createElement ('a')
            const href = window.URL.createObjectURL (data) // 创建下载的链接
            downloadElement.href = href
            downloadElement.download = decodeURI ('绩效考核规则模版.xlsx') // 下载后文件名
            document.body.appendChild (downloadElement)
            downloadElement.click () // 点击下载
            document.body.removeChild (downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL (href) // 释放掉blob对象
            this.loadingPage = false
            // this.$my_message.success ('导出成功')
            this.$message.success ('导出成功')
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
// 获得部门
      getDeptList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/queryAllAssetDept'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            this.deptList = data.result
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      //  考核内容
      getAssessmentContent() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/getAssessmentType'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.assessmentContentList = data
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 类别
      getAategory() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/getCategoryType'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.categoryList = data
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      }

    }
  }
</script>
